package com.tosit.bmx.service.user.Impl;

import com.tosit.bmx.domian.SubjectVo;
import com.tosit.bmx.domian.TempSubject;
import com.tosit.bmx.domian.Temporary;
import com.tosit.bmx.domian.TestMsg;
import com.tosit.bmx.mapper.SubjectMapper;
import com.tosit.bmx.mapper.TemporaryMapper;
import com.tosit.bmx.service.user.TemporaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author tsl
 * @create 2018-04-28 11:28
 **/
@Service
public class TemporaryServiceImpl implements TemporaryService {
    @Autowired
    TemporaryMapper temporaryMapper;
    @Autowired
    SubjectMapper subjectMapper;

    /**
     * 通过所选套题获取题目
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/28 9:52
     */
    @Override
    public List<SubjectVo> getSubjectBySeIds(List<Integer> seids) {
        return subjectMapper.selectSubjectBySeIds(seids);
    }

    @Override
    @Transactional
    public List<TempSubject> initTest(List<Integer> seids, Integer type, Integer userId) {
        Integer result = temporaryMapper.deletes(userId);
        List<SubjectVo> subjectVos = subjectMapper.selectSubjectBySeIds(seids);
        List<TempSubject> subjects = new ArrayList<>();
//        Integer[] sort = new Integer[subjectVos.size()];
//        List<Integer> list = null;
//        for (int i = 0; i < subjectVos.size(); i++) {
//            sort[i] = i + 1;
//        }
//        if (type == 1) {
//            list = Arrays.asList(sort);
//            Collections.shuffle(list);
//        }
        if (type == 1) {
            Collections.shuffle(subjectVos);
        }
        for (int i = 0; i < subjectVos.size(); i++) {
            TempSubject tempSubject = new TempSubject();
            tempSubject.setSubjectVo(subjectVos.get(i));
            tempSubject.setSsid(subjectVos.get(i).getSecondSubId());
            tempSubject.setUid(userId);
            tempSubject.setTime(new Date().getTime() / 1000 + "");
            tempSubject.setTorder(i);
            subjects.add(tempSubject);
        }
//        if (type == 1) {
//            Collections.sort(subjects, new Comparator<TempSubject>() {
//                @Override
//                public int compare(TempSubject o1, TempSubject o2) {
//                    return o1.getTorder() - o2.getTorder();
//                }
//            });
//        }
        temporaryMapper.insertByTempSubject(subjects);
        return subjects;
    }

    @Override
    public List<TempSubject> contineTest(Integer id) {
        return temporaryMapper.selectTempSubject(id);
    }

    @Override
    @Transactional
    public Integer modifyTem(Temporary temporary) {
        return temporaryMapper.updateByTorder(temporary);
    }

    @Override
    public TestMsg getCarCourse(Integer id) {
        return temporaryMapper.getTestMsg(id);
    }

    @Override
    public TestMsg getCarCourseq(Integer ssid) {
        return temporaryMapper.getTestMsgg(ssid);
    }

    @Override
    public List<TempSubject> getRadomSub(Integer courId, Integer id) {
        Integer result = temporaryMapper.deletes(id);
        List<TempSubject> subjects = new ArrayList<>();
        List<SubjectVo> subjectVos = subjectMapper.selectSubjectByCourses(courId, id);
        for (int i = 0; i < subjectVos.size(); i++) {
            TempSubject tempSubject = new TempSubject();
            tempSubject.setSubjectVo(subjectVos.get(i));
            tempSubject.setSsid(subjectVos.get(i).getSecondSubId());
            tempSubject.setUid(id);
            tempSubject.setTime(new Date().getTime() / 1000 + "");
            tempSubject.setTorder(i);
            subjects.add(tempSubject);
        }
        temporaryMapper.insertByTempSubject(subjects);
        return subjects;
    }

    @Override
    public Temporary isExsit(Integer id) {
        return temporaryMapper.findByUserId(id);
    }

    @Override
    public Integer deleteTem(List<Integer> torders, Integer id) {
       Integer result= temporaryMapper.deletesBytorder(torders,id);
        return result;
    }

    @Override
    public Integer deleteByUserId(Integer id) {
        return temporaryMapper.deletes(id);
    }
}
